
// MFCApplication1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCApplication1.h"
#include "MFCApplication1Dlg.h"
#include "afxdialogex.h"
//#include <Shellapi.h>     // Included for shell constants such as FO_* values
//#include <shlobj.h>       // Required for necessary shell dependencies
//#include <strsafe.h>      // Including StringCch* helpers

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//typedef struct
//{
//	WORD    cbSize;
//	WORD    cchUrl;      // Store the length of the URL.
//	WCHAR   pwszUrl[1];  // Store the URL.
//} URLIDL;
//// CMFCApplication1Dlg dialog



CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFCApplication1Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_CEdit);
	DDX_Control(pDX, IDC_EDIT2, m_CEditFile);
}

BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &CMFCApplication1Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication1Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CMFCApplication1Dlg message handlers

BOOL CMFCApplication1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCApplication1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//#pragma warning(disable : 4996)
//HRESULT CreateIDL(CString &strUrl, ITEMIDLIST** ppidl)
//{
//	HRESULT hr = S_OK;
//
//	// Check assertions.
//	_ASSERTE(ppidl);
//
//	// Initialize the return value.
//	*ppidl = NULL;
//
//	// Get the size of the URL
//	WORD cb;
//	WORD cbUrl = strUrl.GetLength() * sizeof(WCHAR);
//	cb = (WORD)sizeof(URLIDL) + cbUrl;
//
//	// Allocate an additional WORD, which is the "next"
//	// PIDL (one of cb==0).
//	URLIDL* pURLIDL = (URLIDL*)LocalAlloc(LPTR, cb + sizeof(WORD));
//	hr = (pURLIDL ? S_OK : E_FAIL);
//	if (SUCCEEDED(hr))
//	{
//		pURLIDL->cbSize = cb;
//		// Store the number of characters in the URL.
//		pURLIDL->cchUrl = (WORD)strUrl.GetLength(); 
//
//		// NOTE: CopyChars copies the exact number of characters
//		// *including* the terminating null character.
//
//		CString::CopyChars(
//			pURLIDL->pwszUrl, 
//			(LPCTSTR)strUrl, 
//			(strUrl.GetLength() + 1));
//
//		// Terminate the IDL.
//		ITEMIDLIST *pidlNext = (ITEMIDLIST *)
//			(((LPBYTE)pURLIDL) + pURLIDL->cbSize);
//		*((WORD *)pidlNext) = 0;
//
//		// Return a clone of the IDL.
//		*ppidl = ILClone((ITEMIDLIST*)pURLIDL);
//		hr = (*ppidl ? S_OK : E_OUTOFMEMORY);
//
//		// Free the IDL.
//		LocalFree((HLOCAL)pURLIDL);
//	}
//
//	return hr;
//}


void CMFCApplication1Dlg::OnBnClickedButton1()
{
	CString arguments =L"jesseprotocol://";
	
	CString sWindowText;
	m_CEdit.GetWindowTextW(sWindowText);
	arguments.Append(sWindowText);
	ShellExecute(this->GetSafeHwnd(),L"Open",arguments,L"",L"",SW_SHOWNORMAL); 
	
	//IShellItemArray* shellItemArray=NULL;
	//ITEMIDLIST* itemidlist=NULL;
	//hr=CreateIDL(arguments,&itemidlist);
	//hr=SHCreateShellItemArrayFromIDLists(1,(LPCITEMIDLIST*)itemidlist,&shellItemArray);
	////pIdlArray[0] = myFirstPidl;
	////SHCreateShellItemArrayFromIDLists(1,,&shellItemArray)
	//CoInitialize(NULL);
	//IApplicationActivationManager* paam = NULL;
	//hr = CoCreateInstance(CLSID_ApplicationActivationManager,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&paam));
	//if (FAILED(hr)) return ;
	//DWORD pid = 0;
	//hr = paam->ActivateForProtocol(appId,shellItemArray,&pid);
	//if (FAILED(hr)) return ;
	//wprintf(L"Activated  %s with pid %d\r\n",appId,pid);
	//CoUninitialize();
}




void CMFCApplication1Dlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
}


void CMFCApplication1Dlg::OnBnClickedButton2()
{
	// appUserModelId find in the regedit
	LPCWSTR appId = L"7ae4aac2-a9c6-411c-b274-d94f741e8dbd_d6t0nyhvsvkqp!App";
	
	CoInitialize(NULL);
	IApplicationActivationManager* paam = NULL;
	HRESULT hr = E_FAIL;
	__try
	{
		hr = CoCreateInstance(CLSID_ApplicationActivationManager,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&paam));
		if (FAILED(hr)) return ;

		DWORD pid = 0;
		// No arguments, it can only work at the first time
        hr = paam->ActivateApplication(appId,nullptr,AO_NOSPLASHSCREEN ,&pid);
		if (FAILED(hr)) return ;

		wprintf(L"Activated  %s with pid %d\r\n",appId,pid);
	}
	__finally
	{
		if (paam) paam->Release();
	}

	CoUninitialize();

}
